VFG.EditEPolyline=function(viewer,entity){
	this.viewer=viewer;
	this.tip=new VFG.Tip(viewer);
	this.entity=entity;
	this.editPointMap=new Map();
	this.init();
}
VFG.EditEPolyline.prototype.init=function(){
	var _this=this;
	_this.createPoint();
	
	this._handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	var pickEntity;
	var index;
	//鼠标左键按下
	this._handler.setInputAction(function(e) {
		let pick = _this.viewer.scene.pick(e.position);
		if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id && pick.id.id.indexOf("-EditLinePoint")!=-1){
			pickEntity=pick;
			index=_this.getIndex(pick.id.id);
			pickEntity.id.point.color=Cesium.Color.RED;
	 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
		}
	},Cesium.ScreenSpaceEventType.LEFT_DOWN);
    
	this._handler.setInputAction(function(e) {
		if(pickEntity){
			pickEntity.id.point.color=Cesium.Color.YELLOW;
		}
		_this.viewer.scene.screenSpaceCameraController.enableRotate = true;
		_this.tip.setVisible(false);
		pickEntity=null;
		index=null;
	},Cesium.ScreenSpaceEventType.LEFT_UP);
	
	this._handler.setInputAction(function(movement) {
		if(pickEntity){
			let cartesian3=VFG.Util.getScreenToC3 (_this.viewer,movement.endPosition,pickEntity.id) 
	        if(Cesium.defined(cartesian3)){
	        	pickEntity.id.position=cartesian3;
	        	_this.entity.thing.positions[index]=cartesian3;
	        	_this.entity.updatePositions(_this.entity.thing.positions, true);
	    		if(_this.change){
	    			_this.change(_this.entity.thing.positions);
	    		}
	        }
		}
	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this._handler.setInputAction(function(movement) {
		_this.destroy();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
	
	this._handler.setInputAction(function(movement) {
		let pick = _this.viewer.scene.pick(movement.position);
		if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id && pick.id.id.indexOf("-EditLinePoint")!=-1){
			index=_this.getIndex(pick.id.id);
			if(Cesium.defined(index)){
				_this.addIndex(index);
			}
		}
	}, Cesium.ScreenSpaceEventType.MIDDLE_DOWN );
	
	this._handler.setInputAction(function(movement) {
		let pick = _this.viewer.scene.pick(movement.position);
		if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id && pick.id.id.indexOf("-EditLinePoint")!=-1){
			index=_this.getIndex(pick.id.id);
			_this.removeIndex(index);
		}
	}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
	
}

VFG.EditEPolyline.prototype.createPoint = function() {
	var positions=this.entity.thing.positions;
	this.pointArray=[];
	var i=0;
	for(var position of positions){
		var id=VFG.Util.getUuid()+"-EditLinePoint";
		var point=this.viewer.entities.add({
			id:id,
	        position: position,
	        point:{
	        	show: true, 
	            pixelSize: 6,
	            color: Cesium.Color.YELLOW, 
	        }
	    });
		this.pointArray.push(point);
		i++;
	}
};

VFG.EditEPolyline.prototype.getIndex = function(id) {
	for(var i=0;i<this.pointArray.length;i++){
		if(this.pointArray[i].id==id){
			return i;
		}
	}
};

VFG.EditEPolyline.prototype.removeIndex= function(index) {
	var _this = this;
	_this.viewer.entities.remove(_this.pointArray[index]);
	_this.pointArray.splice(index,1);
	_this.entity.thing.positions.splice(index,1);
	_this.entity.updatePositions(_this.entity.thing.positions, true);
	if(_this.change){
		_this.change(_this.entity.thing.positions);
	}
};

VFG.EditEPolyline.prototype.addIndex= function(index) {
	var _this = this;
	var length=_this.pointArray.length;
	var firstPos;
	var secondPos;
	var insertIndex;
	if(index+1<length){
		firstPos=this.entity.thing.positions[index];
		secondPos=this.entity.thing.positions[index+1];
		insertIndex=index+1;
	}else{
		firstPos=this.entity.thing.positions[index-1];
		secondPos=this.entity.thing.positions[index];
		insertIndex=index;
	}
    var midpoint = Cesium.Cartesian3.midpoint(firstPos, secondPos, new Cesium.Cartesian3());
	var id=VFG.Util.getUuid()+"-EditLinePoint";
	var point=this.viewer.entities.add({
		id:id,
        position: midpoint,
        point:{
        	show: true, 
            pixelSize: 6,
            color: Cesium.Color.YELLOW, 
        }
    });
	_this.pointArray.splice(insertIndex, 0, point);
	_this.entity.thing.positions.splice(insertIndex, 0, midpoint);
	_this.entity.updatePositions(_this.entity.thing.positions, true);
	if(_this.change){
		_this.change(_this.entity.thing.positions);
	}
	
};

VFG.EditEPolyline.prototype.on = function(type,callback) {
	if('change'==type){
		this.change=callback;
	}
	if('destroy'==type){
		this.destroy=callback;
	}
};

VFG.EditEPolyline.prototype.destroy = function() {
    if(this.pointArray){
    	for(var point of this.pointArray){
    		this.viewer.entities.remove(point);
    	}
    }
    if(this.destroy){
    	this.destroy();
    }
    if( this._handler){
    	this._handler.destroy();
    	this._handler = null;
    };
    this.entity.updatePositions(this.entity.thing.positions, false);
    if(this.tip){
    	this.tip.destroy();
    }
    delete this.viewer;
	return Cesium.destroyObject(this);
};
